//
//  MBStatisticsViewModel.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBStatisticsViewModel.h"
#import "MBStatisticsTableCellViewModel.h"
#import "MBStatisticsNetTool.h"
#import "MBSaleRankModel.h"

@implementation MBStatisticsViewModel

-(void)setUpData
{
    [super setUpData];
    
    NSDate *now = [NSDate date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *dd = [cal components:unitFlags fromDate:now];
    NSInteger year = [dd year];
    NSInteger month = [dd month];

    self.year = year;
    self.monthy = month;
    self.monthy2 = month;
    self.year2 = year;
}

-(void)sendRequestWithType:(int)type result:(MBRequestResult)result
{
    if (type == 0) {
        [self getStoreProfile];
    }else if(type == 1){
        [self getStoreReport];
    }else {
        MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
        
        MBSalesParam* param = [[MBSalesParam alloc]init];
        param.store_id = userModel.store_id;
        param.p = self.currentPage;
        param.token = userModel.token;
        [MBStatisticsNetTool statisticsNetToolGetSalesListWithParam:param success:^(MBHTTPResponse *response) {
            if (response.status == 1) {
                NSArray* array = response.result;
                NSMutableArray* list = [NSMutableArray array];
                
                for (int i=0; i<array.count; i++) {
                    MBSaleRankModel* model = [MBSaleRankModel yy_modelWithDictionary:array[i]];
                    model.num = i+1;
                    [list addObject:model];
                }
                [self factoryInitDataModelArray:list totalCount:10 cellViewModelClass:[MBStatisticsTableCellViewModel class]];
                
                result(YES);
            }else {
                [InterfaceHUD showErrorHUDWithString:response.msg];
                result(NO);
            }
        } failure:^(NSError *error) {
            [InterfaceHUD showErrorHUDWithString:@"获取商品失败，请稍后重试！"];
            result(NO);
        }];
    }
}

-(void)getStoreProfile
{
    MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    MBSalesParam* param = [[MBSalesParam alloc]init];
    param.begin_time = [NSString stringWithFormat:@"%ld-%ld",(long)self.year,(long)self.monthy];
    if (self.monthy<10) {
        param.begin_time = [NSString stringWithFormat:@"%ld-0%ld",(long)self.year,(long)self.monthy];
    }
    param.store_id = userModel.store_id;
    param.token = userModel.token;
    [MBStatisticsNetTool statisticsNetToolGetStoreProfileWithParam:param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            MBStatisticsProfileModel* model = [MBStatisticsProfileModel yy_modelWithDictionary:response.result];
            self.profileModel = model;
        }else {
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
        self.endRefresh = 1;
    } failure:^(NSError *error) {
        [InterfaceHUD showErrorHUDWithString:@"获取店铺概况失败，请稍后重试！"];
        self.endRefresh = 1;
    }];
}

-(void)getStoreReport
{
    MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    MBSalesParam* param = [[MBSalesParam alloc]init];
    param.begin_time = [NSString stringWithFormat:@"%ld-%ld",(long)self.year2,(long)self.monthy2];
    if (self.monthy2 <10) {
        param.begin_time = [NSString stringWithFormat:@"%ld-0%ld",(long)self.year2,(long)self.monthy2];
    }
    param.store_id = userModel.store_id;
    param.token = userModel.token;
    [MBStatisticsNetTool statisticsNetToolGetStoreReportWithParam:param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            MBStatisticsReportModel* model = [MBStatisticsReportModel yy_modelWithDictionary:response.result];
            self.reportModel = model;
        }else {
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
        self.endRefresh = 2;
    } failure:^(NSError *error) {
        self.endRefresh = 2;
        [InterfaceHUD showErrorHUDWithString:@"获取运营报告失败，请稍后重试！"];
    }];
}

-(NSInteger)numberOfSections
{
    return 1;
}

-(NSInteger)numberOfRowInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)heightForHeaderInSection:(NSInteger)section
{
    return [UIView lf_sizeFromIphone6:17];
}

-(CGFloat)heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(MBBaseTableCellViewModel *)cellViewModelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.dataArray[indexPath.row];
}

@end
